package com.example.forum.service.imp;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.exception.ApplicationException;
import com.example.forum.mapper.BoardMapper;
import com.example.forum.model.Board;
import com.example.forum.service.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class BoardServiceImp implements IBoardService {
    @Autowired
    private BoardMapper boardMapper;

    @Override
    public List<Board> selectByNum(Integer num) {
        //进行num的校验
        if (num <= 0) {
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        List<Board> boards = boardMapper.selectByNum(num);
        return boards;
    }

    @Override
    public Board selectById(Long id) {
        if (id == null || id <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        // 调用DAO查询数据
        Board board = boardMapper.selectByPrimaryKey(id);
        // 返回结果
        return board;
    }

    @Override
    public void subOneArticleCountById(Long id) {
        //首先判断id参数的合法性
        if(id == null || id <= 0){
            log.warn("没有找到对应的板块，id：" + id);
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //根据id查询对应的板块
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board == null){
            //此时板块为空
            log.warn(ResultCode.ERROR_IS_NULL.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.ERROR_IS_NULL));
        }

        //构造更新对象
        Board updataBoard = new Board();
        updataBoard.setId(board.getId());
        if(board.getArticleCount() == 0){
            updataBoard.setArticleCount(0);
        }else {
            updataBoard.setArticleCount(board.getArticleCount() - 1);
        }

        int result = boardMapper.updateByPrimaryKeySelective(updataBoard);

        //判断这里的更新是否出现了的错误
        if(result != 1){
            log.warn(ResultCode.FAILED.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED));
        }

    }

    @Override
    public void addOneArticleCountById(Long id) {
        //首先判断id参数的合法性
        if(id == null || id <= 0){
            log.warn("没有找到对应的板块，id：" + id);
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //根据id查询对应的板块
        Board board = boardMapper.selectByPrimaryKey(id);
        //校验是否正确
        if(board == null){
            //此时板块为空
            log.warn(ResultCode.ERROR_IS_NULL.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.ERROR_IS_NULL));
        }

        //更新这里的文章的数量
        Board updataBoard = new Board();
        updataBoard.setId(board.getId());
        updataBoard.setArticleCount(board.getArticleCount() + 1);

        int result = boardMapper.updateByPrimaryKeySelective(updataBoard);

        //判断这里的更新是否出现了的错误
        if(result != 1){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.ERROR_SERVICES));
        }
    }
}
